﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication.Code;
using Company.Merchandising.ServiceLayer.EntityManagement;
using Company.Merchandising.DataAccessLayer.DataAccessCommon;
using Telerik.Web.UI;
using System.Drawing;
using WebApplication.Controls.Management.Company;
using Company.Merchandising.BusinessLayer.DataTransferObjects;

namespace WebApplication.Restricted.Management.Company
{
	public partial class ContactPersonManagement : EntityPage
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void ContactPeopleGrid_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			ContactPersonEditControl contactPersonControl = editedItem.FindControl(GridEditFormItem.EditFormUserControlID) as ContactPersonEditControl;
			if (contactPersonControl == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			ContactPersonManager provinceManager = new ContactPersonManager(ModelEntities);
			try
			{
				provinceManager.AddConcactPerson(contactPersonControl.EditedContactPerson);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}

		protected void ContactPeopleGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
		{
			ContactPeopleGrid.DataSource = new DACContactPerson(ModelEntities).GetAllContactPersons();
		}

		protected void ContactPeopleGrid_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			if (editedItem["Id"] == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			int editedContactPersonId;
			if (!int.TryParse(editedItem["Id"].Text, out editedContactPersonId))
			{
				AddErrorMessage("Bląd wewnętrzny");
				e.Canceled = true;
				return;
			}

			ContactPersonEditControl contactPersonControl = editedItem.FindControl(GridEditFormItem.EditFormUserControlID) as ContactPersonEditControl;
			if (contactPersonControl == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				return;
			}

			DTOContactPerson dtoContactPerson = contactPersonControl.EditedContactPerson;
			dtoContactPerson.Id = editedContactPersonId;
			ContactPersonManager contactPersonManager = new ContactPersonManager(ModelEntities);
			try
			{
				contactPersonManager.UpdateContactPerson(dtoContactPerson);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}

		protected void ContactPeopleGrid_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
		{
			GridEditableItem editedItem = e.Item as GridEditableItem;
			if (editedItem == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			if (editedItem["Id"] == null)
			{
				AddErrorMessage("Błąd wewnętrzny");
				e.Canceled = true;
				return;
			}

			int editedContactPersonId;
			if (!int.TryParse(editedItem["Id"].Text, out editedContactPersonId))
			{
				AddErrorMessage("Bląd wewnętrzny");
				e.Canceled = true;
				return;
			}

			ContactPersonManager contactPersonManager = new ContactPersonManager(ModelEntities);
			try
			{
				contactPersonManager.DeleteContactPerson(editedContactPersonId);
			}
			catch (Exception ex)
			{
				AddErrorMessage(ex.Message);
				e.Canceled = true;
			}
		}

		private void AddErrorMessage(string errorMessage)
		{
			Label errorLabel = new Label();
			errorLabel.Text = errorMessage;
			errorLabel.ForeColor = Color.Red;

			ContactPeopleGrid.Controls.Add(errorLabel);
		}
	}
}
